/**
 * 
 */
package gui.view.source;

import engine.Service;
import gui.controller.SourceMainListener;
import gui.controller.SourceRightListener;
import gui.view.CustomizedColor;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import massDistribution.MeasuredAttribute;
import stubs.ServiceStub;
import stubs.TrustLevel;

/**
 * @author Elisa Costante
 * 
 */
public class SourceRightPanel extends JPanel {

	private static final long serialVersionUID = 2361169235615027671L;
	private JLabel selectedSourceLabel;
	private JLabel attributeListLabel;
	private JTable attributeListTable;
	private JScrollPane attributeListScroll;
	private JTable servicesListTable;
	private JScrollPane servicesListScroll;
	private JButton okButton;
	private JButton editServiceButton;
	private JLabel servicesListLabel;
	private MyTableModel attributeTableModel;
	private MyTableModel servicesTableModel;
	private JComboBox trustLevelCombo;
	private JPanel attributeForServicePanel;
	private JButton doneButton;
	private JButton resetAttributeButton;
	private JButton resetButton;
	private Hashtable<String, MeasuredAttribute> attributeList;
	private SourceRightListener rightListener;
	private SourceMainListener mainListener;
	private JPanel servicePanel;

	public SourceRightPanel() {
		super();
	}

	/**
	 * Initialize the panel and the pane commponents
	 */
	public void init(SourceRightListener rightListener,
			SourceMainListener mainListener) {
		this.rightListener = rightListener;
		this.mainListener = mainListener;
		this.setBackground(CustomizedColor.BACK_GROUND_COLOR);
		setRightComponents();
		setRightPanelLayout();
	}

	private void setRightComponents() {

		setServicePanel();

		setAttributePanel();

	}

	private void setServicePanel() {
		servicePanel = new JPanel();
		servicePanel.setBackground(CustomizedColor.BACK_GROUND_COLOR);
		selectedSourceLabel = new JLabel("Selected Source");

		trustLevelCombo = new JComboBox(new String[] {
				TrustLevel.LOW.getLabel(), TrustLevel.MEDIUM.getLabel(),
				TrustLevel.HIGH.getLabel() });
		trustLevelCombo.setSelectedIndex(0);

		servicesListLabel = new JLabel("Services List");

		servicesListScroll = new JScrollPane(getServicesListTable());
		getServicesListTable().setFillsViewportHeight(true);

		editServiceButton = new JButton("Edit");
		editServiceButton.setActionCommand("editService");
		editServiceButton.addActionListener(rightListener);

		resetButton = new JButton("Reset");
		resetButton.setActionCommand("resetServices");
		resetButton.addActionListener(rightListener);

		okButton = new JButton("Ok");
		okButton.setActionCommand("ok");
		okButton.addActionListener(mainListener);

		setServicePanelLayout();
	}

	private void setServicePanelLayout() {
		GroupLayout layout = new GroupLayout(servicePanel);
		servicePanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		SequentialGroup horizontal = layout.createSequentialGroup();

		horizontal.addComponent(servicesListScroll, 100, 100, Short.MAX_VALUE);

		ParallelGroup rightButtonHorizontal = layout.createParallelGroup();

		rightButtonHorizontal.addComponent(resetButton, 100, 100, 100);
		rightButtonHorizontal.addComponent(editServiceButton, 100, 100, 100);
		rightButtonHorizontal.addComponent(okButton, 100, 100, 100);

		horizontal.addGroup(rightButtonHorizontal);

		ParallelGroup vertical = layout.createParallelGroup();

		vertical.addComponent(servicesListScroll, 100, 100, Short.MAX_VALUE);

		SequentialGroup rightButtonVertical = layout.createSequentialGroup();
		rightButtonVertical.addComponent(resetButton, 30, 30, 30);
		rightButtonVertical.addComponent(editServiceButton, 30, 30, 30);
		rightButtonVertical.addComponent(okButton, 30, 30, 30);

		vertical.addGroup(rightButtonVertical);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	private void setRightPanelLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		ParallelGroup horizontal = layout.createParallelGroup();

		ParallelGroup servicesHorizontalGroup = layout.createParallelGroup();
		servicesHorizontalGroup.addComponent(servicesListLabel, 100, 100,
				Short.MAX_VALUE);
		servicesHorizontalGroup.addComponent(servicePanel, 200, 300,
				Short.MAX_VALUE);

		ParallelGroup attrHorizontalGroup = layout.createParallelGroup();
		attrHorizontalGroup.addComponent(attributeListLabel, 100, 100,
				Short.MAX_VALUE);
		attrHorizontalGroup.addComponent(attributeForServicePanel);

		horizontal.addComponent(selectedSourceLabel, 100, 100, Short.MAX_VALUE);
		horizontal.addGroup(servicesHorizontalGroup);
		horizontal.addGroup(attrHorizontalGroup);

		SequentialGroup vertical = layout.createSequentialGroup();

		SequentialGroup servicesVerticalGroup = layout.createSequentialGroup();
		servicesVerticalGroup.addComponent(servicesListLabel, 30, 30, 30);
		servicesVerticalGroup.addComponent(servicePanel, 100, 200,
				Short.MAX_VALUE);

		SequentialGroup attrVerticalGroup = layout.createSequentialGroup();
		attrVerticalGroup.addComponent(attributeListLabel, 30, 30, 30);
		attrVerticalGroup.addComponent(attributeForServicePanel, 100, 200,
				Short.MAX_VALUE);

		vertical.addComponent(selectedSourceLabel);
		vertical.addGroup(servicesVerticalGroup);
		vertical.addGroup(attrVerticalGroup);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	private void setAttributePanel() {
		attributeForServicePanel = new JPanel();
		attributeForServicePanel
				.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		attributeListLabel = new JLabel("Attribute List");
		attributeListScroll = new JScrollPane(getAttributeListTable());
		getAttributeListTable().setFillsViewportHeight(true);

		doneButton = new JButton("Done");
		doneButton.setActionCommand("done");
		doneButton.addActionListener(rightListener);

		resetAttributeButton = new JButton("Reset");
		resetAttributeButton.setActionCommand("resetAttribute");
		resetAttributeButton.addActionListener(rightListener);

		setAttributPanelLayout();
	}

	/**
	 * 
	 */
	private void setAttributPanelLayout() {
		GroupLayout layout = new GroupLayout(attributeForServicePanel);
		attributeForServicePanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		SequentialGroup horizontal = layout.createSequentialGroup();

		horizontal.addComponent(attributeListScroll, 100, 100, Short.MAX_VALUE);

		ParallelGroup rightButtonHorizontal = layout.createParallelGroup();

		rightButtonHorizontal.addComponent(resetAttributeButton, 100, 100, 100);
		rightButtonHorizontal.addComponent(doneButton, 100, 100, 100);
		horizontal.addGroup(rightButtonHorizontal);

		ParallelGroup vertical = layout.createParallelGroup();

		vertical.addComponent(attributeListScroll, 100, 100, Short.MAX_VALUE);

		SequentialGroup rightButtonVertical = layout.createSequentialGroup();
		rightButtonVertical.addComponent(resetAttributeButton, 30, 30, 30);
		rightButtonVertical.addComponent(doneButton, 30, 30, 30);
		vertical.addGroup(rightButtonVertical);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);
	}

	private JTable getAttributeListTable() {
		if (attributeListTable == null) {
			attributeListTable = new JTable();
			setDefaultAtttributeTableModel();
		}
		return attributeListTable;
	}

	/**
	 * @return
	 */
	public JTable getServicesListTable() {
		if (servicesListTable == null) {
			servicesListTable = new JTable();
			setDefaultServiceTableModel();
			servicesListTable
					.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			servicesListTable.setRowSelectionAllowed(true);
			servicesListTable.setColumnSelectionAllowed(false);

			// Add a Row Selection Listener

			servicesListTable.getSelectionModel().addListSelectionListener(
					rightListener);

		}
		return servicesListTable;
	}

	/**
	 * @return the selectedSourceLabel
	 */
	public JLabel getSelectedSourceLabel() {
		return this.selectedSourceLabel;
	}

	/**
	 * @return the attributeListLabel
	 */
	public JLabel getAttributeListLabel() {
		return this.attributeListLabel;
	}

	/**
	 * @return the attributeListScroll
	 */
	public JScrollPane getAttributeListScroll() {
		return this.attributeListScroll;
	}

	/**
	 * @return the servicesListScroll
	 */
	public JScrollPane getServicesListScroll() {
		return this.servicesListScroll;
	}

	/**
	 * @return the okButton
	 */
	public JButton getOkButton() {
		return this.okButton;
	}

	/**
	 * @return the cancelButton
	 */
	public JButton getCancelButton() {
		return this.editServiceButton;
	}

	/**
	 * @return the servicesListLabel
	 */
	public JLabel getServicesListLabel() {
		return this.servicesListLabel;
	}

	/**
	 * Set the monitores attributes list model of a source
	 * 
	 * @param monitoredAttributes
	 */
	public void setAttributeTableModelList(
			ArrayList<MeasuredAttribute> monitoredAttributes,
			boolean allSelected) {
		if (monitoredAttributes != null) {

			setDefaultAtttributeTableModel();
			attributeTableModel.setRowCount(monitoredAttributes.size());

			for (int i = 0; i < monitoredAttributes.size(); i++) {
				MeasuredAttribute attribute = monitoredAttributes.get(i);
				attributeTableModel.setValueAt(attribute.getIdentifier(), i, 0);
				// attributeTableModel.setValueAt(attribute.getMetric().getName(),
				// i, 1);
				attributeTableModel.setValueAt(new Boolean(allSelected), i, 1);

			}

		} else
			setDefaultAtttributeTableModel();

	}

	public void setDefaultAtttributeTableModel() {
		attributeTableModel = new MyTableModel(new String[] { "Attribute Name",
				"Selected" }, 0);
		attributeListTable.setModel(attributeTableModel);

	}

	/**
	 * Set the monitored services list model of a source
	 * 
	 * @param monitoredServices
	 */
	public void setServicesTableModelList(ArrayList<Service> monitoredServices,
			boolean AllSelected) {
		if (monitoredServices != null && monitoredServices.size() > 0) {
			setDefaultServiceTableModel();
			servicesTableModel.setRowCount(monitoredServices.size());
			for (int i = 0; i < monitoredServices.size(); i++) {
				ServiceStub service = (ServiceStub) monitoredServices.get(i);
				servicesTableModel
						.setValueAt(service.getIdentifierName(), i, 0);
				servicesTableModel.setValueAt(new Boolean(AllSelected), i, 1);
				if (service.getTrustValue() != null)
					servicesTableModel.setValueAt(service.getTrustValue()
							.getLabel(), i, 2);
				else
					servicesTableModel.setValueAt(TrustLevel.LOW.getLabel(), i,
							2);

			}
		} else
			setDefaultServiceTableModel();

	}

	public void setDefaultServiceTableModel() {
		servicesTableModel = new MyTableModel(new String[] { "Service Name",
				"Selected", "Trust Level" }, 0);
		servicesListTable.setModel(servicesTableModel);
		servicesTableModel.setUpComboColumn(servicesListTable, trustLevelCombo);

	}

	/**
	 * @return the trustLevelCombo
	 */
	public JComboBox getTrustLevelCombo() {
		return this.trustLevelCombo;
	}

	/**
	 * @return the attributeForServicePanel
	 */
	public JPanel getAttributeForServicePanel() {
		return this.attributeForServicePanel;
	}

	/**
	 * @return the attributeList
	 */
	public Hashtable<String, MeasuredAttribute> getAttributeList() {
		return this.attributeList;
	}

	/**
	 * @param attributeList
	 *            the attributeList to set
	 */
	public void setAttributeList(
			Hashtable<String, MeasuredAttribute> attributeList) {
		this.attributeList = attributeList;
	}

	/**
	 * @return the attributeTableModel
	 */
	public DefaultTableModel getAttributeTableModel() {
		return this.attributeTableModel;
	}

	/**
	 * @return the servicesTableModel
	 */
	public DefaultTableModel getServicesTableModel() {
		return this.servicesTableModel;
	}

	private class MyTableModel extends DefaultTableModel {

		private static final long serialVersionUID = 2509271465955677214L;

		private MyTableModel(Object[] columnNames, int rowCount) {
			super(columnNames, rowCount);
		}

		public void setUpComboColumn(JTable jtable, JComboBox combo) {

			TableColumn comboColumn;
			comboColumn = jtable.getColumnModel().getColumn(2);
			comboColumn.setCellEditor(new DefaultCellEditor(combo));
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.TableModel#getColumnClass(int)
		 */
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			if (getColumnName(columnIndex).equalsIgnoreCase("selected"))
				return Boolean.class;
			else
				return super.getColumnClass(columnIndex);
		}

	}

	/**
	 * @return the servicePanel
	 */
	public JPanel getServicePanel() {
		return this.servicePanel;
	}

}
